package com.linkage.bss.crm.packagemgr.action;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.linkage.bss.commons.dao.Page;
import com.linkage.bss.crm.packagemgr.model.Area;
import com.linkage.bss.crm.packagemgr.model.PackParamType;
import com.linkage.bss.crm.packagemgr.smo.IPackageUtilSMO;
import com.linkage.bss.crm.packagemgr.util.DataGridManager;

/**
 * 包公共Action类
 *
 * @ClassName: PromoUtilAction.java
 * @Description: PromoUtilAction.java
 * @author: JinShuming
 * @date 2012-8-13
 */
public class PackageUtilAction extends DataGridManager {
	private static final long serialVersionUID = 1L;

	private static JSONArray catalogArray;

	private IPackageUtilSMO packageUtilSMO = null;

	public String queryProdSpec() {
		List<Map> list = null;
		String prodName = ServletActionContext.getRequest().getParameter("prodName");
		String prodSpecId = ServletActionContext.getRequest().getParameter("prodSpecId");
		//封装查询条件
		Map<String, Object> queryMap = new HashMap<String, Object>();
		try {
			if (prodName != null && !prodName.equals("")) {
				prodName = URLDecoder.decode(prodName, "UTF-8");
				queryMap.put("prodName", prodName);
			}
			if (prodSpecId != null && !prodSpecId.equals("")) {
				queryMap.put("prodSpecId", prodSpecId);
			}

			int total = packageUtilSMO.queryProdSpecCnt(queryMap);

			Page queryPage = new Page();

			queryPage.setTotalSize(total);
			queryPage.setPageSize(rows);
			queryPage.init();
			queryPage.setCurPage(page);

			List<Map> prodSpecList = packageUtilSMO.queryProdSpec(queryMap, queryPage);

			Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map
			jsonMap.put("total", total);//total键 存放总记录数，必须的
			jsonMap.put("rows", prodSpecList);//rows键 存放每页记录 list

			//json转化属性过滤，忽略掉值为null的属性
			result = JSONObject.fromObject(jsonMap, jsonConfig);//格式化result   一定要是JSONObject
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 非分页查询产品规格
	 * @return [参数说明]
	 *
	 * @return String [返回类型说明]
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public String queryProdSpecList() {
		List<Map> list = null;
		String prodName = ServletActionContext.getRequest().getParameter("prodName");
		String prodSpecId = ServletActionContext.getRequest().getParameter("prodSpecId");
		//封装查询条件
		Map<String, Object> queryMap = new HashMap<String, Object>();
		try {
			if (prodName != null && !prodName.equals("")) {
				prodName = URLDecoder.decode(prodName, "UTF-8");
				queryMap.put("prodName", prodName);
			}
			if (prodSpecId != null && !prodSpecId.equals("")) {
				queryMap.put("prodSpecId", prodSpecId);
			}

			List<Map> prodSpecList = packageUtilSMO.queryProdSpec(queryMap);

			arrayResult = JSONArray.fromObject(prodSpecList, jsonConfig);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String queryOfferSpec() {
		List<Map> list = null;
		String offerSpecName = ServletActionContext.getRequest().getParameter("offerSpecName");
		String partObjId = ServletActionContext.getRequest().getParameter("partObjId");
		String actionType = ServletActionContext.getRequest().getParameter("actionType");
		String offerSpecIds = ServletActionContext.getRequest().getParameter("offerSpecIds");
		//封装查询条件
		Map<String, Object> queryMap = new HashMap<String, Object>();
		if (offerSpecName != null && !offerSpecName.equals("")) {
			try {
				offerSpecName = URLDecoder.decode(offerSpecName, "UTF-8");
				offerSpecName = offerSpecName.replaceAll("[ \\s|\'　\']+", "%");// 将空格用“%”替换
				queryMap.put("offerSpecName", offerSpecName);
			} catch (UnsupportedEncodingException e) {

				e.printStackTrace();
			}
		}
		queryMap.put("partObjId", partObjId);
		queryMap.put("actionType", actionType);
		queryMap.put("offerSpecIds", offerSpecIds);
		int total = packageUtilSMO.queryOfferSpecCnt(queryMap);

		Page queryPage = new Page();

		queryPage.setTotalSize(total);
		queryPage.setPageSize(rows);
		queryPage.init();
		queryPage.setCurPage(page);

		List<Map> offerSpecList = packageUtilSMO.queryOfferSpec(queryMap, queryPage);

		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map
		jsonMap.put("total", total);//total键 存放总记录数，必须的
		jsonMap.put("rows", offerSpecList);//rows键 存放每页记录 list

		//json转化属性过滤，忽略掉值为null的属性
		result = JSONObject.fromObject(jsonMap, jsonConfig);//格式化result   一定要是JSONObject

		return SUCCESS;
	}

	public String queryParamType() {
		List<PackParamType> paramTypes = packageUtilSMO.queryParamType();
		Map<String, Object> jsonMap = new HashMap<String, Object>();// 定义map
		jsonMap.put("paramTypes", paramTypes);

		// json转化属性过滤，忽略掉值为null的属性
		result = JSONObject.fromObject(jsonMap, jsonConfig);// 格式化result

		// 一定要是JSONObject
		return SUCCESS;
	}

	public void setPackageUtilSMO(IPackageUtilSMO packageUtilSMO) {
		this.packageUtilSMO = packageUtilSMO;
	}

	public String getAreaByParentID() throws UnsupportedEncodingException {

		// 封装查询条件

		String areaId = ServletActionContext.getRequest().getParameter("areaId3Level");
		Integer areaIdInt = Integer.valueOf(areaId);

		List<Area> areas = packageUtilSMO.getAreaByParentID(areaIdInt);

		arrayResult = getPackageJsonArray(areas);
		return SUCCESS;
	}

	/**
	 * 根据包编码查询包的个数
	 *
	 * @return int [返回类型说明]
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public String isPackCodeSingle() {
		String packCode = ServletActionContext.getRequest().getParameter("packCode");
		int size = packageUtilSMO.isPackCodeSingle(packCode);

		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map
		jsonMap.put("size", size);

		result = JSONObject.fromObject(jsonMap, jsonConfig);

		return SUCCESS;
	}

	/**
	 * 根据销售品ID获得
	 * 销售品的角色名称
	 * @param offerSpecId
	 * @return [参数说明]
	 *
	 * @return List [返回类型说明]
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public String queryOfferRoleNameById() {
		String offerSpecId = ServletActionContext.getRequest().getParameter("offerSpecId");
		List list = packageUtilSMO.queryOfferRoleNameById(Long.valueOf(offerSpecId));

		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map
		jsonMap.put("roles", list);

		result = JSONObject.fromObject(jsonMap, jsonConfig);

		return SUCCESS;
	}

	private JSONArray getPackageJsonArray(List<Area> areas) {

		List<Map<String, Object>> items = new ArrayList<Map<String, Object>>();

		if (areas != null) {
			Iterator<Area> iter = areas.iterator();
			while (iter.hasNext()) {
				Area node = iter.next();
				Map<String, Object> item = new HashMap<String, Object>();
				item.put("id", node.getAreaId());
				item.put("text", node.getName());

				items.add(item);
			}
		}
		return JSONArray.fromObject(items);
	}

	/**
	 *查询促销 标签
	* queryPromoLabel
	* @param
	* @return
	 */
	public String queryPromoLabel() {
		List<Map> list = null;
		String labelName = ServletActionContext.getRequest().getParameter("labelName");
		//封装查询条件
		Map<String, Object> queryMap = new HashMap<String, Object>();
		try {
			if (labelName != null && !labelName.equals("")) {
				labelName = URLDecoder.decode(labelName, "UTF-8");
				queryMap.put("labelName", labelName);
			}

			int total = packageUtilSMO.queryPromoLabel(queryMap);

			Page queryPage = new Page();

			queryPage.setTotalSize(total);
			queryPage.setPageSize(rows);
			queryPage.init();
			queryPage.setCurPage(page);

			List<Map> labelList = packageUtilSMO.queryPromoLabel(queryMap, queryPage);

			Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map
			jsonMap.put("total", total);//total键 存放总记录数，必须的
			jsonMap.put("rows", labelList);//rows键 存放每页记录 list

			//json转化属性过滤，忽略掉值为null的属性
			result = JSONObject.fromObject(jsonMap, jsonConfig);//格式化result   一定要是JSONObject
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

}
